package com.yangtao.springbootfreemarker.config;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Method;

/**
 * 使用缓存
 */
@Configuration
@EnableCaching
public class CachingConfig {

	/**
	 * 生成缓存KEY
	 * @return
	 */
	@Bean
	public KeyGenerator keyGenerator() {
		return new KeyGenerator() {
			@Override
			public Object generate(Object target, Method method, Object... params) {
				StringBuilder sb = new StringBuilder(16);
				sb.append(target.getClass().getName());
				sb.append("_");
				sb.append(method.getName());
				sb.append("_");
				for (int i = 0; i < params.length; i++) {
					sb.append(params[i]);
					if (i < params.length - 1) {
						sb.append(",");
					}
				}
				System.out.println(sb.toString());
				return sb.toString();
			}
		};
	}
}
